package com.zhiyou100.www.bbs.dao;

import com.zhiyou100.www.bbs.entityclass.TableUser;
import com.zhiyou100.www.bbs.function.jdbctool.C3P0_Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;

/**
 * @packageName: TableUserDao
 * @className: com.zhiyou100.www.bbs.dao
 * @Description: TODO 用户类 Dao
 * @author: yang
 * @date: 2020/6/4
 */
public class TableUserDao {

    /**
     * @Description: TODO 获取所有的用户表
     * @name: getAll
     * @param: []
     * @return: java.util.ArrayList<com.zhiyou100.www.bbs.entityclass.TableUser>
     * @date: 2020/6/6 5:46 下午
     * @author: yang
     */
    public ArrayList<TableUser> getAll() {

        ArrayList<TableUser> tableUsers = new ArrayList<>();
        // 创建一个ArrayList，用来放所有的用户

        Connection connection = C3P0_Util.getConnection();
        // 获取链接

        String sql = "select * from tableUser";
        // 准备模版

        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            // 发送模版
            ResultSet resultSet = statement.executeQuery();
            // 查询，获取结果集合
            while (resultSet.next()) {
                TableUser tableUser = new TableUser(resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getString(5), resultSet.getString(6), resultSet.getInt(7), resultSet.getInt(8)
                );
                tableUsers.add(tableUser);
                // 取出来
            }
            C3P0_Util.close(connection, statement, resultSet);
            // 关闭链接
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        tableUsers.sort(new Comparator<TableUser>() {
            // 按照 积分 从大到小排列
            @Override
            public int compare(TableUser o1, TableUser o2) {
                if (o1.getUserScore()>o2.getUserScore()){
                    return -1;
                }else {
                    return  0;
                }
            }
        });
        return tableUsers;
    }

    /**
     * @Description: TODO 判断是否登陆成功
     * @name: login
     * @param: [name, pwd]
     * @return: boolean
     * @date: 2020/6/4 4:02 下午
     * @author: yang
     */
    public boolean login(String name, String pwd) {
        try {
            Connection connection = C3P0_Util.getConnection();
            // 获取链接
            String sql = "select * from tableUser where userName=? and userPwd=?";
            // sql 模版
            PreparedStatement statement = connection.prepareStatement(sql);
            // 发送模版
            statement.setString(1, name);
            statement.setString(2, pwd);
            // 设置参数
            ResultSet resultSet = statement.executeQuery();
            // 查询
            boolean next = resultSet.next();
            // 是否有次用户
            C3P0_Util.close(connection, statement, resultSet);
            // 关闭
            return next;
            // 返回
        } catch (SQLException thenables) {
            throw new RuntimeException(thenables);
        }
    }

    /**
     * @Description: TODO 根据用户名 和 密码 获取 id
     * @name: getId
     * @param: [name, pwd]
     * @return: java.lang.Integer
     * @date: 2020/6/6 2:30 下午
     * @author: yang
     */
    public int getId(String name, String pwd) {
        int id = 0;
        Connection connection = C3P0_Util.getConnection();
        // 获取链接
        String sql = "select userId from tableUser where userName =? and  userPwd=?";
        // select userId from tableUser where userName =? and  userPwd=?;
        // 准备模版
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            // 发送模版
            statement.setString(1, name);
            statement.setString(2, pwd);
            // 设置模版
            ResultSet resultSet = statement.executeQuery();
            // 查询，获取结果集合
            while (resultSet.next()) {
                id = resultSet.getInt(1);
                // 取出来
            }
            C3P0_Util.close(connection, statement, resultSet);
            // 关闭链接
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return id;
    }

    /**
     * @Description: TODO 根据id获取一个
     * @name: getOne
     * @param: [id]
     * @return: com.zhiyou100.www.bbs.entityclass.TableUser
     * @date: 2020/6/6 2:30 下午
     * @author: yang
     */
    public TableUser getOne(Integer id) {
        return new UserLoginFormDao().getOneTableUser(id);
    }


}
